package cn.itcast.tanhua.mytanhuaserver.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.convert.Convert;
import cn.itcast.tanhua.mytanhuadubbointerface.api.QuanziApi;
import cn.itcast.tanhua.mytanhuadubbointerface.api.QuanziSearchApi;
import cn.itcast.tanhua.mytanhuaserver.service.QuanziSearchService;
import cn.itcast.tanhua.mytanhuaserver.service.UserService;
import cn.itcast.tanhua.pojo.Publish;
import cn.itcast.tanhua.pojo.User;
import cn.itcast.tanhua.pojo.UserInfo;
import cn.itcast.tanhua.utils.RelativeDateFormat;
import cn.itcast.tanhua.utils.UserThreadLocal;
import cn.itcast.tanhua.vo.PageResult;
import cn.itcast.tanhua.vo.QuanZiVo;
import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 郭文亮
 * @since 2021/12/9 10:55
 */
@Service
public class QuanziSearchServiceImpl implements QuanziSearchService {

    @Reference
    private QuanziApi quanziApi;

    @Autowired
    private UserInfoServiceImpl userInfoService;

    @Autowired
    private UserService userService;

    @Reference
    private QuanziSearchApi quanziSearchApi;

    @Override
    public boolean synchroEsData() throws Exception {
        // 查询数据
        List<Publish> publishList = this.quanziApi.queryAllPublish();
        List<QuanZiVo> quanZiVoList = new ArrayList<>();
        // 3.遍历数据集publishList，将好友动态的信息封装到quanZiVoList
        for (Publish publish : publishList) {
            QuanZiVo quanZiVo = new QuanZiVo();
            quanZiVo.setId(publish.getId().toHexString());
            quanZiVo.setUserId(publish.getUserId());
            quanZiVo.setTextContent(publish.getText());
            quanZiVo.setImageContent(publish.getMedias().toArray(new String[]{}));
            quanZiVo.setCreateDate(RelativeDateFormat.format(new Date(publish.getCreated())));
            quanZiVoList.add(quanZiVo);
        }
        // 4.查询用户信息，主要包括好友的头像、昵称
        List<Object> userIds = CollUtil.getFieldValues(publishList, "userId");
        List<UserInfo> userInfoList = this.userInfoService.queryUserInfoListByUserIds(userIds);

        //当前用户
        User user = UserThreadLocal.get();
        // 5.遍历数据集userInfoList，将好友信息封装到quanZiVoList
        for (QuanZiVo quanZiVo : quanZiVoList) {
            for (UserInfo userInfo : userInfoList) {
                if (userInfo.getUserId().longValue() == quanZiVo.getUserId().longValue()) {
                    quanZiVo.setGender(userInfo.getSex().name().toLowerCase());
                    quanZiVo.setTags(StringUtils.split(userInfo.getTags(), ','));
                    quanZiVo.setNickname(userInfo.getNickName());

                    quanZiVo.setCommentCount(0); //TODO 评论数
                    quanZiVo.setDistance("1.2公里"); //TODO 距离
                    quanZiVo.setHasLiked(this.quanziApi.queryUserIsLike(user.getId(), quanZiVo.getId()) ? 1 : 0); //是否点赞（1是，0否）
                    quanZiVo.setLikeCount(Convert.toInt(this.quanziApi.queryLikeCount(quanZiVo.getId()))); //点赞数
                    quanZiVo.setHasLoved(this.quanziApi.queryUserIsLove(user.getId(), quanZiVo.getId()) ? 1 : 0); //是否喜欢（1是，0否）
                    quanZiVo.setLoveCount(Convert.toInt(this.quanziApi.queryLoveCount(quanZiVo.getId()))); //喜欢数
                    break;
                }
            }
            // TODO: 2021/12/9 通过userId查询是否黑名单
        }
        return this.quanziSearchApi.synchroEsData(quanZiVoList);
    }

    @Override
    public PageResult queryQuanziByKeyword(Integer page, Integer pageSize, String keyword)  {

        try {
            return this.quanziSearchApi.queryQuanziByKeyword(page, pageSize, keyword);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
